/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jFRelatorio.java
 *
 * Created on 28/06/2012, 21:41:06
 */
package sccpa;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author admin
 */
public class jFRelatorio extends javax.swing.JFrame {

    /** Creates new form jFRelatorio */
    public jFRelatorio() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        entityManager1 = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("db_projeto_aracaPU").createEntityManager();
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sccpa.SCCPAApp.class).getContext().getResourceMap(jFRelatorio.class);
        query1 = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery(resourceMap.getString("query1.query")); // NOI18N
        list1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query1.getResultList());
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N

        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(122, 122, 122)
                .addComponent(jButton1)
                .addContainerGap(163, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(56, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
                String caminhoRelJasper = "resources/rAlunos.jasper";
                ResultSet rs = null;

            try {
                Alunos oAluno = new Alunos();
                Connection jpa = ((oracle.toplink.essentials.ejb.cmp3.EntityManager) entityManager1).getServerSession().getDefaultConnectionPool().acquireConnection().getConnection();
                
                rs = oAluno.getImprimir(jpa);
                JRResultSetDataSource ds = new JRResultSetDataSource(rs);
                Map parametros = new HashMap();
                //parametros.put("tagParameter",tag.getText());
                URL arquivo = getClass().getResource(caminhoRelJasper);
                if (arquivo == null)
                JOptionPane.showMessageDialog(null,"Arquivo jasper não encontrado");
                JasperReport report;
            try {

                    report = JasperManager.loadReport(arquivo.openStream());
                    JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap(), ds);
                    JasperViewer jrviewer = new JasperViewer(jasperPrint, false);
                    jrviewer.setVisible(true);


            } catch (JRException ex) {
                JOptionPane.showMessageDialog(null, ex, "ATENÇÃO", JOptionPane.ERROR_MESSAGE);
                Logger.getLogger(jFRelatorio.class.getName()).log(Level.SEVERE, null, ex);
            }

            } catch (SQLException ex) {
            Logger.getLogger(jFRelatorio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex, "ATENÇÃO", JOptionPane.ERROR_MESSAGE);
                Logger.getLogger(jFRelatorio.class.getName()).log(Level.SEVERE, null, ex);
            }

        
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new jFRelatorio().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager entityManager1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private java.util.List list1;
    private javax.persistence.Query query1;
    // End of variables declaration//GEN-END:variables
}
